/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.tabbedproperties.section;

import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.Activity;
import org.eclipse.uml2.uml.ActivityParameterNode;
import org.eclipse.uml2.uml.Parameter;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Section on ActivityParameterNode
 *
 * @author Gilles Cannenterre
 * @since 10 avr. 07
 */
public class ActivityParameterNodeParameterSection extends PropertyViewSection {

	/** The combo type. */
	private CCombo comboParameter;
	
	/** The combo type listener. */
	private SelectionListener comboParameterListener;
	
	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent, TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		Composite composite = getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		getWidgetFactory().createCLabel(composite, "Parameter:"); //$NON-NLS-1$

		comboParameter = getWidgetFactory().createCCombo(composite, SWT.NONE);
		data = new FormData();
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.top = new FormAttachment(0, 0);
		comboParameter.setLayoutData(data);

		comboParameterListener = new SelectionListener () {
			public void widgetSelected(SelectionEvent e) {
				Parameter newParameter = (Parameter)(comboParameter.getData(comboParameter.getItem(comboParameter.getSelectionIndex())));
				if (umlElement instanceof ActivityParameterNode) {
					((ActivityParameterNode)umlElement).setParameter(newParameter);
					if (newParameter != null) {
						((ActivityParameterNode)umlElement).setType(newParameter.getType());
					}
				}				
			}

			public void widgetDefaultSelected(SelectionEvent e) {}
		};
		
		comboParameter.addSelectionListener(comboParameterListener);		
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if(!comboParameter.isDisposed()){
			if (umlElement instanceof ActivityParameterNode) {
				// Get current parameter
				Parameter currentParameter = ((ActivityParameterNode)umlElement).getParameter();
				
				// Clean and fill the combo	
				comboParameter.removeAll();
				// Add undefined element
				int comboIndex = 0;
				comboParameter.add("<Undefined>");
				comboParameter.setData(null);
				comboIndex = comboIndex + 1;
				
				// Add the activityParameterNode's activity parameters
				Activity theActivity = ((ActivityParameterNode)umlElement).getActivity();
				Iterator<Parameter> parameters = theActivity.getOwnedParameters().iterator();
				while(parameters.hasNext()) {
					Parameter aParameter = (Parameter) parameters.next();
					comboParameter.add(aParameter.getName());
					comboParameter.setData(aParameter.getName(), aParameter);		
					// Select the current property type in the list
					if((currentParameter != null) && currentParameter.getName().equals(aParameter.getName())) {
						comboParameter.select(comboIndex);
					}
					comboIndex = comboIndex + 1;
				}
			}
		}

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if (comboParameter!=null && !comboParameter.isDisposed()) {
			comboParameter.removeSelectionListener(comboParameterListener);
		}		
	}
}